<?php
declare (strict_types=1);

namespace app\api\controller\Live;

use app\api\controller\Base;
use app\common\service\lib\BaiduApi;
use app\common\service\live\LiveSpeakService;
use app\validate\Live\LiveSpeakCheck;
use think\App;

class LiveSpeak extends Base
{
    /**
     * 保存用户弹幕
     * Author:William
     */
    public function add()
    {
        $post = request()->post();
        $post['user_id'] = $this->request->uid;
        //验证参数
        $this->validateFail(LiveSpeakCheck::class, $post);
//        检验用户是否被禁言
        $SpeakCheckRes=LiveSpeakService::userVerify($post['user_id']);
        if ($SpeakCheckRes !=true){
            return $this->res($SpeakCheckRes);
        }
//        过滤敏感弹幕内容
        $baidu = new BaiduApi();
        $baiduRes = $baidu->index($post['content']);
        if ($baiduRes ['conclusionType'] !== 1) {
            return $this->result('','400','内容不合格');
        }
        $res = LiveSpeakService::save($post);
        return $this->res($res);
    }

    /**
     * Author:William
     * @return mixed
     */
    public function get(){
        //房间号
        $listKey = config('setting.live.listKey');
        $data=LiveSpeakService::getRedisSpeak($listKey);
        return $this->res($data);
    }
}
